﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace XNAGameFeatures.Input.Device
{
    /// <summary>
    /// 
    /// </summary>
    public class GamePadInput : InputDevice
    {

        #region BaseInput

        private readonly GamePadState[] _current = new GamePadState[MaxInput];
        private readonly GamePadState[] _old = new GamePadState[MaxInput];

        internal GamePadInput()
        {

        }

        /// <summary>
        /// Updates all input entry.
        /// </summary>
        internal override void Update()
        {
            for (int i = 0; i < _current.Length; i++)
            {
                _old[i] = _current[i];

                _current[i] = GamePad.GetState((PlayerIndex)i);
            }
        } 

        #endregion

        #region Public Methods

        /// <summary>
        /// Determines whether [is button press once] [the specified button].
        /// </summary>
        /// <param name="button">The button.</param>
        /// <param name="player">The player.</param>
        /// <returns>
        ///   <c>true</c> if [is button press once] [the specified button]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsButtonPressOnce(Buttons button, PlayerIndex player)
        {
            return _current[(int)player].IsButtonDown(button) && _old[(int)player].IsButtonUp(button);
        }

        /// <summary>
        /// Determines whether [is button press once] [the specified button].
        /// </summary>
        /// <param name="button">The button.</param>
        /// <returns>
        ///   <c>true</c> if [is button press once] [the specified button]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsButtonPressOnce(Buttons button)
        {
            return IsButtonPressOnce(button, PlayerIndex.One)
                || IsButtonPressOnce(button, PlayerIndex.Two)
                || IsButtonPressOnce(button, PlayerIndex.Three)
                || IsButtonPressOnce(button, PlayerIndex.Four);
        }

        /// <summary>
        /// Determines whether [is button press] [the specified button].
        /// </summary>
        /// <param name="button">The button.</param>
        /// <param name="player">The player.</param>
        /// <returns>
        ///   <c>true</c> if [is button press] [the specified button]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsButtonPress(Buttons button, PlayerIndex player)
        {
            return _current[(int)player].IsButtonDown(button);
        }

        /// <summary>
        /// Determines whether [is button press] [the specified button].
        /// </summary>
        /// <param name="button">The button.</param>
        /// <returns>
        ///   <c>true</c> if [is button press] [the specified button]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsButtonPress(Buttons button)
        {
            return IsButtonPress(button, PlayerIndex.One)
                || IsButtonPress(button, PlayerIndex.Two)
                || IsButtonPress(button, PlayerIndex.Three)
                || IsButtonPress(button, PlayerIndex.Four);
        }

        #endregion

    }
}
